package com.grade.system.controller;

import com.grade.system.entity.Grade;
import com.grade.system.mapper.GradeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
public class GradeRestController {

    @Autowired
    private GradeMapper gradeMapper;

    @RequestMapping("/aj_sel_grade_by_all")
    @ResponseBody
    public List<Grade> selGradeByAll() {
        return gradeMapper.selGradeByAll();
    }

    @RequestMapping("/aj_sel_grade_by_id")
    @ResponseBody
    public Grade selGradeById(@RequestParam Map<String, Object> map) {
        int id = Integer.parseInt(String.valueOf(map.get("id")));
        return gradeMapper.selGradeById(id);
    }

    @RequestMapping("/aj_del_grade_by_id")
    @ResponseBody
    public int delGradeById(@RequestParam Map<String, Object> map) {
        int id = Integer.parseInt(String.valueOf(map.get("id")));
        return gradeMapper.delGradeById(id) ? NetState.STATE_OK.value : NetState.STATE_ERROR.value;
    }

    @RequestMapping("/aj_ins_grade")
    @ResponseBody
    public int insGrade(@RequestParam Map<String, Object> map) {
        int student_id = Integer.parseInt(String.valueOf(map.get("student_id")));
        String course_name = String.valueOf(map.get("course_name"));
        double score = Double.parseDouble(String.valueOf(map.get("score")));
        Grade grade = new Grade(student_id, course_name, score);
        return gradeMapper.insGrade(grade) ? NetState.STATE_OK.value : NetState.STATE_ERROR.value;
    }

    @RequestMapping("/aj_upd_grade_by_id")
    @ResponseBody
    public int updGradeById(@RequestParam Map<String, Object> map) {
        int id = Integer.parseInt(String.valueOf(map.get("id")));
        int student_id = Integer.parseInt(String.valueOf(map.get("student_id")));
        String course_name = String.valueOf(map.get("course_name"));
        double score = Double.parseDouble(String.valueOf(map.get("score")));
        Grade grade = new Grade(id, student_id, course_name, score);
        return gradeMapper.updGrade(grade) ? NetState.STATE_OK.value : NetState.STATE_ERROR.value;
    }
}
